﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using Guest_room.BLL;

namespace Guest_room.admin
{
    public partial class department : Form
    {
        public department()
        {
            InitializeComponent();
        }
        SqlDataAdapter daDepartment, daLog;
        DataSet ds = new DataSet();
        GuestRoomService guestRoomService = new GuestRoomService();
        void init()
        {
            DB.GetCn();
            string str = "select * from 部门基本信息表";
            string slr = "select * from 日志表";
            daDepartment = new SqlDataAdapter(str, DB.cn);
            daLog = new SqlDataAdapter(slr, DB.cn);
            daDepartment.Fill(ds, "department_info");
            daLog.Fill(ds, "log_info");
            DB.cn.Close();
        }//创建连接
        void showAll()
        {
            DataView dvDpm = new DataView(ds.Tables["department_info"]);
            dataGridView1.DataSource = dvDpm;
            //字体居中
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列名居中
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }//显示数据
        void showXz()//显示选择列
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            //显示在第一列
            dataGridView1.Columns.Insert(0, acCode);
            //宽度为40
            dataGridView1.Columns[0].Width = 40;
            dataGridView1.Columns[3].Width = 240;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//多选变单选
        {
            if (dataGridView1.Rows.Count > 0)   //判断是否有数据
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历所有行
                {
                    DataGridViewCheckBoxCell ck = dataGridView1.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;    //获取当前行的选择框
                    ck.Value = (i != e.RowIndex) ? false : true;
                }
            }
        }

        private void btnadd_Click(object sender, EventArgs e)//添加
        {
            //判断必填项
            if (text_id.Text == "" || text_name.Text == "")
            {
                MessageBox.Show("带*号为必填项");
            }
            else
            {

                string departmentId = text_id.Text;
                string departmentName = text_name.Text;

                bool isExist = guestRoomService.IsDepartmentExist(departmentId, departmentName);

                if (isExist)
                {
                    MessageBox.Show("部门编号或部门名称已存在");
                }
                else
                {
                    DataRow drEm = ds.Tables["department_info"].NewRow();
                    drEm["部门编号"] = text_id.Text;
                    drEm["部门名称"] = text_name.Text;
                    ds.Tables["department_info"].Rows.Add(drEm);
                    DataRow drLog = ds.Tables["log_info"].NewRow();
                    drLog["操作员"] = lbluser.Text;
                    drLog["操作员编号"] = Home.userid ?? Login.userid;//判断是哪个方式登录
                    drLog["操作时间"] = DateTime.Now;
                    drLog["操作类型"] = "添加";
                    drLog["操作表"] = "部门基本信息表";
                    drLog["操作内容"] = "添加了部门编号为‘" + text_id.Text + "’的部门";
                    ds.Tables["log_info"].Rows.Add(drLog);
                    //跳转至所添加行
                    dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1];
                }

            }

        }

        private void btnReb_Click(object sender, EventArgs e)//撤销
        {
            ds.RejectChanges();
        }

        private void btndel_Click(object sender, EventArgs e)//删除
        {
            bool flag = false;
            string staff_id = "";
            if (dataGridView1.Rows.Count > 0)   //判断是否有数据
            {

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell ck = dataGridView1.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if (ck.Value != null && (bool)ck.Value)
                    {
                        if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            flag = true;
                            staff_id = dataGridView1.Rows[i].Cells["部门名称"].Value.ToString();
                            //查询员工基本信息表和管理员基本信息表中是否有部门编号为staff_id的数据
                            bool hasStaffOrAdmin = guestRoomService.HasStaffOrAdmin(staff_id);

                            if (hasStaffOrAdmin)
                            {
                                MessageBox.Show("该部门下有员工或管理员，不能删除");
                                return;
                            }
                            else
                            {
                                dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                                i--;
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                if (flag)
                {
                    MessageBox.Show("删除成功");
                    if (DB.GetDataSet("select * from 部门基本信息表 where 部门编号='" + staff_id + "'").Rows.Count > 0)//判断受否存在数据库中
                    {
                        DataRow drLog = ds.Tables["log_info"].NewRow();
                        drLog["操作员"] = lbluser.Text;
                        drLog["操作员编号"] = Home.userid ?? Login.userid;//判断是哪个方式登录
                        drLog["操作时间"] = DateTime.Now;
                        drLog["操作类型"] = "删除";
                        drLog["操作表"] = "部门基本信息表";
                        drLog["操作内容"] = "删除了部门名称为‘" + staff_id + "’的部门";
                        ds.Tables["log_info"].Rows.Add(drLog);
                    }
                }
                else
                {
                    MessageBox.Show("请选择要删除的行");
                }
            }
        }

        private void btnsave_Click(object sender, EventArgs e)//保存
        {
            try
            {
                SqlCommandBuilder cb = new SqlCommandBuilder(daDepartment);//自动生成SQL语句
                SqlCommandBuilder cl = new SqlCommandBuilder(daLog);    //自动生成SQL语句
                daDepartment.Update(ds, "department_info"); //更新数据库
                daLog.Update(ds, "log_info");   //更新数据库
                MessageBox.Show("保存成功");
                DB.cn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)//修改
        {

        }

        private void department_Load(object sender, EventArgs e)
        {
            lbluser.Text = Home.username ?? Login.username;
            init();
            showAll();
            showXz();
        }
    }
}
